package code.lewdy.lottery;

import java.util.ArrayList;
import java.util.List;

import javax.mail.Message;

import com.lt.member.Member;
import com.lt.member.MemberService;

import code.lewdy.app.communication.Alert;
import code.lewdy.app.user.AccountRecord;
import code.lewdy.db.HibernateTemple;
import code.lewdy.mail.ICBCMailUtil;
import code.lewdy.mail.MailReceiver;
import code.lewdy.util.RandomUtil;

/**通过邮件扫描自动充值
 * @author Administrator
 *
 */
public class RechargeByMailScan {
	
	List<Message> msgs = new ArrayList<Message>();
	int fetchSize = 10; //每次检索邮件数目
	String subject = "工商";
	private double minPoundageAmount; //补贴手续费最小额度
	private double addPoundageAmount; //补贴手续费
	private String mail;
	private String mailPassword;
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		RechargeByMailScan scan = new RechargeByMailScan();
		//scan.setMail("lewdy007@sina.com");
		//scan.setMailPassword("lewdy123");
		scan.setMail("newdoo@sina.com");
		scan.setMailPassword("thejiang");
		scan.scanning();
		
	}
	
	public void scanning(){
		System.out.println(toString());
		MailReceiver mr = new MailReceiver("pop.sina.com", mail, mailPassword,
				"INBOX", "pop3");
		msgs =  mr.fetchSubjectMessagesDESC(subject, fetchSize);
		String text = null;
		for(Message msg : msgs){
			text = ICBCMailUtil.getMailHTMLText(msg);
			if(text==null){
				continue;
			}
			System.out.println("HTML:"+text);
			MailRechargeRecord mrr = ICBCMailUtil.analysICBCText(text);
			if(null == mrr)
				continue;
			if((Long)HibernateTemple.getUniqueResult("select count(*) from MailRechargeRecord where " +
					" serialNo='"+mrr.getSerialNo()+"'") > 0){
				continue; //记录已存在,跳过
			}
			Member m = (Member) HibernateTemple.load(Member.class, Integer.parseInt(""+mrr.getOwnerId()));
			if(m == null){
				mrr.setNote("充值处理失败：该用户不存在!");
				HibernateTemple.save(mrr);
				continue;
			}
			//判断收款账号是否为指定账号
			String receiverCard = "*"+mrr.getReceiverCardNo();
			Long noCount = (Long) HibernateTemple.getUniqueResult("select count(*) from BankAccountConfig where " +
					" cardNo='"+receiverCard+"'");
System.out.println("long count:"+noCount);
			if(noCount==null || noCount<=0){
				System.out.println("收款账号不是指定收款账号!");
				mrr.setNote("充值处理失败：收款账号不是指定收款账号!");
				HibernateTemple.save(mrr);
				continue;
			}else{
				System.out.println("收款账号正确!");
			}
			mrr.setNote("充值处理成功!");
			mrr.setOwnerName(m.getUsername());
			HibernateTemple.save(mrr);
			//更新账户余额
			double amount = mrr.getAmount();
			if(amount>= this.minPoundageAmount){
				amount += mrr.getBankPoundage();
			}
			MemberService.updateBalance(m, amount, RandomUtil.getRandomString(18), 
					AccountRecord.GROUP_RECHARGE_BY_ONLINE, "在线自动充值");
			new Alert().save(m, "在线自动充值成功", "您好，你通过网上银行转账充值"+amount+"元已到账!");
		}
		mr.close();
	}

	public void setMinPoundageAmount(double minPoundageAmount) {
		this.minPoundageAmount = minPoundageAmount;
	}

	public double getMinPoundageAmount() {
		return minPoundageAmount;
	}

	public void setAddPoundageAmount(double addPoundageAmount) {
		this.addPoundageAmount = addPoundageAmount;
	}

	public double getAddPoundageAmount() {
		return addPoundageAmount;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getMail() {
		return mail;
	}

	public void setMailPassword(String mailPassword) {
		this.mailPassword = mailPassword;
	}

	public String getMailPassword() {
		return mailPassword;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "RechargeByMailScan{mailAddress="+this.mail+",maillPassword="+this.mailPassword+"," +
				"minPoundageAmount="+this.minPoundageAmount+"}";
	}
	
	

}
